//Kristo's own miscellaneous
package src{

import flash.display.*;
import flash.events.*;
import flash.geom.ColorTransform;

public class Mi extends MovieClip{

	static var clickEvent = "click";
	static var overEvent = "mouseOver";
	static var downEvent = "mouseDown";
	static var upEvent = "mouseUp";
	static var outEvent = "mouseOut";
	static var moveEvent = "mouseMove";
	
	public static var stageW = 800;
	public static var stageH = 480;
	public static var mid = {x:400,y:240};
	public static var fps = 40;
	
	public static function addMovie(targetMov:MovieClip, parentM:MovieClip, pos:Object = null):MovieClip{
		parentM.addChild(targetMov);
		if(pos != null){
			for(var i in pos){
				targetMov[i] = pos[i];
			}
		}
		return targetMov;
	}
	public static function removeMovie(targetMov:MovieClip):void{
		targetMov.parent.removeChild(targetMov);
	}
	public static function addEventC(targetMov, func:Function, bypassSound = false):void{
		addEvent(targetMov, clickEvent, func, true, bypassSound);
		targetMov.mouseChildren = false;
	}

	public static function addEvent(targetMov, type:String, func:Function, movAsBtn = false, bypassSound = false):void{
		if(targetMov != null){
			if(movAsBtn && targetMov is MovieClip){
				//perlakukan movie clip sebagai button
				movieClipAsButton(targetMov);
			}
			
			if(!bypassSound && (targetMov is SimpleButton || movAsBtn)){
				//jika berupa button atau movie clip ini mau diperlakukan sebagai button, 
				//maka beri aksi ketika button itu diklik akan bunyi
				targetMov.addEventListener(downEvent, playBtnSound);
			}
			targetMov.addEventListener(type, func);
			function targetRemoved(event){
				//ketika movie clip ini telah dihapus dari stage, maka hapus juga semua eventnya
				targetMov.removeEventListener(type, func);
				targetMov.removeEventListener(Event.REMOVED_FROM_STAGE, targetRemoved);
				targetMov.removeEventListener(downEvent, playBtnSound);
				targetMov = null;
			}
			targetMov.addEventListener(Event.REMOVED_FROM_STAGE,targetRemoved);
		}else trace("Add event failed, target null");
	}


	public static function movieClipAsButton(target:MovieClip):void{
		target.buttonMode = true;
		addEvent(target, overEvent, buttonGlowEv);
		addEvent(target, outEvent, buttonUnglowEv);
	}

	public static function removeAsButton(target:MovieClip):void{
		target.buttonMode = false;
		
		target.removeEventListener(downEvent, buttonGlowEv);
		target.removeEventListener(upEvent, buttonUnglowEv);
		target.removeEventListener(outEvent, buttonUnglowEv);
		target.removeEventListener(downEvent, playBtnSound);
	}

	public static function initBtn(btn:MovieClip, textT:String, onClick:Function = null){
		if(textT != null && btn.infoT != null){
			btn.infoT.text = textT;
		}
		if(onClick != null) addEventC(btn, onClick);
	}
	
	public static function removeFromArr(obj:Object, array:Array):Boolean{
		//removes the obj from array, if exists
		for(var i=0;i<array.length;i++){
			if(obj == array[i]){
				array.splice(i,1);
				return true;
			}
		}
		return false;
	}
	public static function delay(mov:DisplayObject,delaySec:Number, actionAfter:Function):void{
	//delay before executing function.. action after receives mov parameter
		var count = 0;
		function timeControl(){
			if(count++ >= delaySec*fps){
				//jika waktu sudah habis, maka function akan dieksekusi
				mov.removeEventListener(Event.ENTER_FRAME, timeControl);
				actionAfter(mov);
			}
		}
		addEvent(mov, Event.ENTER_FRAME, timeControl);
	}
	public static function intRandom(rangeLow:Number,rangeHigh:Number):Number{
		return Math.floor(Math.random()*(rangeHigh+1-rangeLow)+rangeLow);
	}
	public static function floatRandom(rangeLow:Number,rangeHigh:Number):Number{
		return Math.random()*(rangeHigh-rangeLow)+rangeLow;
	}
	public static function round(num:Number, digit:Number):Number{
		digit = Math.pow(10,digit);
		return Math.round(num*digit)/digit;
	}
	public static function trim(s:String):String{
		//remove beginning and trailing space/enter
		return s.replace(/^\s+|\s+$/gs, '');
	}
	static function playBtnSound(e):void{
	}
	static function buttonGlowEv(e):void{
		buttonGlow(e.currentTarget);
	}
	static function buttonUnglowEv(e):void{
		buttonUnglow(e.currentTarget);
	}

	static function buttonGlow(btn):void{
		btn.transform.colorTransform = new ColorTransform(1,1,1,btn.alpha,-45,-45,-45,0);
	}
	static function buttonUnglowArr(btnArr:Array):void{
		for(var i=0;i<btnArr.length;i++){
			buttonUnglow(btnArr[i]);
		}
	}
	static function buttonUnglow(btn):void{
		btn.transform.colorTransform = new ColorTransform(1,1,1,btn.alpha,0,0,0,0);
	}
	

}
}